﻿package mortal.game.view.common
{
    import com.gengine.global.*;
    import com.mui.controls.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.game.resource.*;

    public class NumInput extends GSprite
    {
        private var _numTxt:GTextInput;
        private var _minNum:int = 1;
        private var _maxNum:int = 2147483647;
        private var _currnetNum:int;
        private var _isSubDown:Boolean = false;
        private var _isAddDown:Boolean = false;
        private var _count:int = 0;
        private var _stepNumber:int = 1;
        private var _subBtn:GLoadedButton;
        private var _addBtn:GLoadedButton;
        private var _editable:Boolean;

        public function NumInput()
        {
            this.init();
            return;
        }// end function

        public function set editable(param1:Boolean) : void
        {
            this._editable = param1;
            this._subBtn.enabled = this._editable;
            this._numTxt.editable = this._editable;
            this._addBtn.enabled = this._editable;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            this._subBtn = UIFactory.gLoadedButton(ImagesConst.sub3_upSkin, 0, 1, 16, 16, this);
            this._subBtn.configEventListener(MouseEvent.CLICK, this.onSubBtnClickHandler);
            this._subBtn.configEventListener(MouseEvent.MOUSE_DOWN, this.onSubBtnMouseDownHandler);
            this._subBtn.drawNow();
            var _loc_1:* = GlobalStyle.textFormatBai.center();
            this._numTxt = UIFactory.gTextInput(23, 0, 40, 20, this);
            this._numTxt.configEventListener(Event.CHANGE, this.onNumChangeHandler);
            this._numTxt.setStyle("textFormat", _loc_1);
            this._addBtn = UIFactory.gLoadedButton(ImagesConst.sup3_upSkin, 68, 1, 16, 16, this);
            this._addBtn.configEventListener(MouseEvent.CLICK, this.onAddBtnClickHandler);
            this._addBtn.configEventListener(MouseEvent.MOUSE_DOWN, this.onAddBtnMouseDownHandler);
            this._addBtn.drawNow();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._subBtn.removeEventListener(MouseEvent.CLICK, this.onSubBtnClickHandler);
            this._subBtn.removeEventListener(MouseEvent.MOUSE_DOWN, this.onSubBtnMouseDownHandler);
            this._numTxt.removeEventListener(Event.CHANGE, this.onNumChangeHandler);
            this._addBtn.removeEventListener(MouseEvent.CLICK, this.onAddBtnClickHandler);
            this._addBtn.removeEventListener(MouseEvent.MOUSE_DOWN, this.onAddBtnMouseDownHandler);
            this._subBtn.dispose(param1);
            this._numTxt.dispose(param1);
            this._addBtn.dispose(param1);
            this._subBtn = null;
            this._numTxt = null;
            this._addBtn = null;
            this._minNum = 1;
            this._maxNum = int.MAX_VALUE;
            this._currnetNum = 0;
            this._stepNumber = 1;
            this._editable = true;
            this.onRemoveEnterframe();
            super.disposeImpl(param1);
            return;
        }// end function

        public function get editable() : Boolean
        {
            return this._editable;
        }// end function

        private function init() : void
        {
            return;
        }// end function

        public function onSubBtnClickHandler(event:MouseEvent) : void
        {
            if (this._stepNumber == 1)
            {
                var _loc_2:String = this;
                var _loc_3:* = this._currnetNum - 1;
                _loc_2._currnetNum = _loc_3;
            }
            else
            {
                this._currnetNum = this._currnetNum - this._stepNumber;
            }
            this.setInRange(this._currnetNum);
            return;
        }// end function

        public function onSubBtnMouseDownHandler(event:MouseEvent) : void
        {
            this._isSubDown = true;
            addEventListener(Event.ENTER_FRAME, this.onEnterframeHandler);
            Global.stage.addEventListener(MouseEvent.MOUSE_UP, this.onRemoveEnterframe);
            return;
        }// end function

        public function onNumChangeHandler(event:Event) : void
        {
            this.setInRange(parseInt(this._numTxt.text));
            return;
        }// end function

        private function setInRange(param1:int) : void
        {
            param1 = param1 < this._maxNum ? (param1) : (this._maxNum);
            param1 = param1 < this._minNum ? (this._minNum) : (param1);
            this._currnetNum = param1;
            this._numTxt.text = this._currnetNum.toString();
            this.dispatchEvent(new Event(Event.CHANGE));
            return;
        }// end function

        public function onAddBtnClickHandler(event:MouseEvent) : void
        {
            if (this._stepNumber == 1)
            {
                var _loc_2:String = this;
                var _loc_3:* = this._currnetNum + 1;
                _loc_2._currnetNum = _loc_3;
            }
            else
            {
                this._currnetNum = this._currnetNum + this._stepNumber;
            }
            this.setInRange(this._currnetNum);
            return;
        }// end function

        public function onAddBtnMouseDownHandler(event:MouseEvent) : void
        {
            this._isAddDown = true;
            addEventListener(Event.ENTER_FRAME, this.onEnterframeHandler);
            Global.stage.addEventListener(MouseEvent.MOUSE_UP, this.onRemoveEnterframe);
            return;
        }// end function

        public function onRemoveEnterframe(event:Event = null) : void
        {
            Global.stage.removeEventListener(MouseEvent.MOUSE_UP, this.onRemoveEnterframe);
            removeEventListener(Event.ENTER_FRAME, this.onEnterframeHandler);
            this._count = 0;
            this._isAddDown = false;
            this._isSubDown = false;
            return;
        }// end function

        public function onEnterframeHandler(event:Event) : void
        {
            var _loc_2:String = this;
            var _loc_3:* = this._count + 1;
            _loc_2._count = _loc_3;
            if (this._count > 6)
            {
                if (this._isAddDown)
                {
                    if (this._stepNumber == 1)
                    {
                        var _loc_2:String = this;
                        var _loc_3:* = this._currnetNum + 1;
                        _loc_2._currnetNum = _loc_3;
                    }
                    else
                    {
                        this._currnetNum = this._currnetNum + this._stepNumber;
                    }
                    this.setInRange(this._currnetNum);
                }
                if (this._isSubDown)
                {
                    if (this._stepNumber == 1)
                    {
                        var _loc_2:String = this;
                        var _loc_3:* = this._currnetNum - 1;
                        _loc_2._currnetNum = _loc_3;
                    }
                    else
                    {
                        this._currnetNum = this._currnetNum - this._stepNumber;
                    }
                    this.setInRange(this._currnetNum);
                }
            }
            return;
        }// end function

        public function set maxNum(param1:int) : void
        {
            this._maxNum = param1;
            this._currnetNum = this._maxNum;
            this._numTxt.text = this._currnetNum.toString();
            return;
        }// end function

        public function get currentNum() : int
        {
            return this._currnetNum;
        }// end function

        public function set currentNum(param1:int) : void
        {
            this.setInRange(param1);
            return;
        }// end function

        public function get minNum() : int
        {
            return this._minNum;
        }// end function

        public function set minNum(param1:int) : void
        {
            this._minNum = param1;
            this._currnetNum = param1;
            this._numTxt.text = this._currnetNum.toString();
            return;
        }// end function

        public function set stepNumber(param1:int) : void
        {
            this._stepNumber = param1 <= 1 ? (1) : (param1);
            return;
        }// end function

        public function setStyleName(param1:String) : void
        {
            if (param1 == "PageButton")
            {
                this._addBtn.styleName = "NextPageButton";
                this._addBtn.width = 21;
                this._addBtn.height = 20;
                this._addBtn.drawNow();
                this._subBtn.styleName = "PrevPageButton";
                this._subBtn.width = 21;
                this._subBtn.height = 20;
                this._subBtn.drawNow();
            }
            return;
        }// end function

        public function setMaxNum(param1:int) : void
        {
            this._maxNum = param1;
            return;
        }// end function

        override public function set width(param1:Number) : void
        {
            this._numTxt.width = param1 - this._subBtn.width - this._addBtn.width - 10;
            this.onResize();
            return;
        }// end function

        override public function set height(param1:Number) : void
        {
            this._numTxt.height = param1;
            this.onResize();
            return;
        }// end function

        public function onResize() : void
        {
            this._subBtn.y = (this._numTxt.height - this._subBtn.height) / 2;
            this._addBtn.x = this._subBtn.width + this._numTxt.width + 10;
            this._addBtn.y = (this._numTxt.height - this._subBtn.height) / 2;
            return;
        }// end function

    }
}
